<template>
  <div class="dict-container">
    <el-row>
      <el-col :span="24">
        <el-button :disabled="buttomIsDisabled($route,'add')" type="primary" @click="handleClick(null,0,'新增字典')">新增</el-button>
        <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="30%">
          <el-form
            label-position="left"
            label-width="100px"
            :model="dictForm"
            :rules="rules"
            ref="dictForm"
          >
            
            <el-form-item label="字典类型" prop="type">
              <el-select
               :disabled="formType === 1"
                v-model="dictForm.type"
                @change="checkDictType"
                filterable
                allow-create
                default-first-option
                placeholder="请选择字典类型"
              >
                <el-option
                  v-for="item in dictTypes"
                  :key="item.type"
                  :label="item.type"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="字典名称" prop="typeName">
              <el-input
                :disabled="formType === 1"
                v-model="dictForm.typeName"
                placeholder="请输入内容"
                clearable
              />
            </el-form-item>
            <el-form-item label="数据标签" prop="label">
              <el-input
                :disabled="formType === 1"
                v-model="dictForm.label"
                placeholder="请输入内容"
                clearable
              />
            </el-form-item>
            <el-form-item label="数据键值" prop="value">
              <el-input
                :disabled="formType === 1"
                v-model="dictForm.value"
                placeholder="请输入内容"
                clearable
              />
            </el-form-item>
            <el-form-item prop="sort" label="排序">
              <el-input-number
                :disabled="formType === 1"
                v-model="dictForm.sort"
                :min="1"
                label="描述文字"
              ></el-input-number>
            </el-form-item>
            <el-form-item prop="status" label="状态">
              <el-switch
                :disabled="formType === 1"
                v-model="dictForm.status"
                active-value="0"
                inactive-value="1"
                active-color="#13ce66"
                inactive-color="#ff4949"
              ></el-switch>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button
              v-if="formType !== 1"
              :loading="loading"
              type="primary"
              @click="confirmHandel('dictForm')"
            >确 定</el-button>
          </span>
        </el-dialog>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-table :data="pagination.dictDatas" row-key="id" border>
          <el-table-column type="index" width="30px"></el-table-column>
          <el-table-column prop="label" label="数据标签"></el-table-column>
          <el-table-column prop="value" label="数据键值"></el-table-column>
          <el-table-column prop="type" label="字典类型"></el-table-column>
          <el-table-column prop="typeName" label="字典名称"></el-table-column>
          <el-table-column prop="status" label="状态">
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.status === '0' ? 'success' : 'danger'"
                disable-transitions
              >{{scope.row.status === '0'? '启用': '禁用'}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="150px">
            <template slot-scope="scope">
              <el-button @click="handleClick(scope.row,1,'字典详情')" type="text" size="small">查看</el-button>
              <el-button @click="handleClick(scope.row,2,'编辑字典')" type="text" size="small">编辑</el-button>
              <el-button @click="delDict(scope.row)" type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagination.currentPage"
          :page-sizes="pagination.pageSizeChoose"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
        ></el-pagination>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { add, getPaginationList, getTypes, update, del} from "@/api/admin/dict";

export default {
  name: "Dict",
  created() {
     this.getPaginationDicts();
  },
  data() {
    return {
      formType: 0,
      loading: false,
      dialogVisible: false,
      dialogTitle: "",
      dictTypes: [],
      dictForm: {
        type: "",
        typeName: "",
        label: "",
        value: "",
        sort: 1,
        status: "0",
      },
      pagination: {
        currentPage: 1,
        pageSize: 10,
        pageSizeChoose: [5, 10, 15, 20],
        total: 0,
        dictDatas: [],
      },
      rules: {
        type: [{ required: true, message: "请输入类型", trigger: "blur" }],
        typeName: [{ required: true, message: "请输入名称", trigger: "blur" }],
        value: [{ required: true, message: "请输入数据键值", trigger: "blur" }],
        label: [{ required: true, message: "请输入数据标签", trigger: "blur" }],
      },
    };
  },
  watch: {
    dialogVisible: function () {
      if (this.dialogVisible === false) {
        this.$refs["dictForm"].resetFields();
      }
    },
  },
  methods: {
    checkDictType(data){
      if(data.typeName){
        this.dictForm.typeName = data.typeName
        this.dictForm.type = data.type
      }else{
        this.dictForm.type = data
      }
    },
    getDictTypes(){
      getTypes().then(res => {
        this.dictTypes = res.data
      })
    },
    addDict() {
      this.loading = true;
      add(this.dictForm)
        .then(() => {
          this.$message({
            message: "新增成功",
            type: "success",
          });
          this.dialogVisible = false;
          this.loading = false;
          this.getPaginationDicts();
        })
        .catch(() => {
          this.loading = false;
        });
    },
    updateDict() {
      this.loading = true;
      update(this.dictForm)
        .then(() => {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.dialogVisible = false;
          this.loading = false;
          this.getPaginationDicts();
        })
        .catch(() => {
          this.loading = false;
        });
    },
    delDict(dict) {
      this.$confirm(
        '是否确认删除类型名："' + dict.type + '" 标签："'+dict.label+ '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return del(dict.id);
        })
        .then(() => {
          this.$message({
            message: "删除成功",
            type: "success",
          });
         this.getPaginationDicts();
        })
    },
    handleClick(data, type, title) {
      if (type !== 0) {
        this.dictForm = JSON.parse(JSON.stringify(data));
      }
      if(type !== 1){
        this.getDictTypes()
      }
      this.dialogTitle = title;
      this.formType = type;
      this.dialogVisible = true;
    },
    getPaginationDicts() {
      const params = {
        pageSize: this.pagination.pageSize,
        pageIndex: this.pagination.currentPage,
      };
      getPaginationList(params).then((res) => {
        this.pagination.dictDatas = res.data.records;
        this.pagination.total = res.data.total;
      });
    },
    handleSizeChange(val) {
      this.pagination.pageSize = val;
      this.getPaginationDicts();
    },
    handleCurrentChange(val) {
      this.pagination.currentPage = val;
      this.getPaginationDicts();
    },
    confirmHandel(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.formType === 0) {
            this.addDict();
          } else if (this.formType === 2) {
            this.updateDict();
          }
          this.loading = true;
        }
      });
    },
  },
};
</script>

<style>
</style>